﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.Win32;

namespace SysTrayManager
{
    public partial class FormTray : Form
    {
        private NotifyIcon trayIcon;
        private ContextMenu trayMenu;


        public FormTray()
        {
            InitializeComponent();
            trayMenu = new ContextMenu();
            trayMenu.MenuItems.Add("Refresh", OnRefresh);
            trayMenu.MenuItems.Add("Standings", OnStandings);
            trayMenu.MenuItems.Add("Stats", OnStats);
            trayMenu.MenuItems.Add("Exit", OnExit);

            // Create a tray icon. In this example we use a
            // standard system icon for simplicity, but you
            // can of course use your own custom icon too.
            trayIcon = new NotifyIcon();
            trayIcon.Text = "IGG Manager";
            trayIcon.Icon = Properties.Resources.baseball;

            // Add menu to tray icon and show it.
            trayIcon.ContextMenu = trayMenu;
            trayIcon.Visible = true;
        }

        protected override void OnLoad(EventArgs e)
        {
            Visible       = false; // Hide form window.
            ShowInTaskbar = false; // Remove from taskbar.
            base.OnLoad(e);
        }

        private void OnExit(object sender, EventArgs e)
        {
            SetRegistryAction("QUIT");
            Application.Exit();
        }

        private void OnRefresh(object sender, EventArgs e)
        {
            SetRegistryAction("REFRESH");
        }

        private void OnStandings(object sender, EventArgs e)
        {
            SetRegistryAction("STANDINGS");
        }

        private void OnStats(object sender, EventArgs e)
        {
            SetRegistryAction("STATS");
        }

        private void SetRegistryAction(string action)
        {
            try
            {
                RegistryKey myKey = Registry.LocalMachine.OpenSubKey("SOFTWARE\\igg\\NextActions", true);
                if (myKey == null)
                {
                    myKey = Registry.LocalMachine.CreateSubKey("SOFTWARE\\igg\\NextActions");
                }
                if (myKey != null)
                {
                    myKey.SetValue("Action", action, RegistryValueKind.String);
                    myKey.Close();
                }
            }
            catch (Exception ex)
            {
                trayIcon.BalloonTipText = ex.StackTrace;
                trayIcon.BalloonTipTitle = ex.Message;
                trayIcon.ShowBalloonTip(10);
            }
        }
    }
}
